package com.fresh.bus.chat.business;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.canoe.session.SessionRoleManager;
import com.fresh.bus.chat.ctrl.OpCodes;
import com.fresh.bus.chat.persist.UserDao;

@Component
public class WealthService {

	public static final int WEEK_STAR_FEIJI = 3;// 获得周明星飞机
	public static final int ADD_FlOWER = 2;// 获得红豆
	public static final int FEIJI_YUEDING = 2;// 遵守飞机约定
	public static final int REGISTER_QQ = 2;//注册QQ号码
	public static final int ADD_GUANZHU = 3;// 被关注
	public static final int REMOVE_GUANZHU = -3;// 失去关注

	private static final Logger log = LoggerFactory
			.getLogger(WealthService.class);
	private StringRedisTemplate template;

	@Resource
	private UserDao userDao;
	@Resource
	protected SessionRoleManager roleManager;
	@Resource
	private UserService userService;
	@Resource
	private MessageService msgService;

	@Autowired
	public WealthService(StringRedisTemplate template) {
		this.template = template;
	}

	public void update(int amount, int value, String user) {
		int newValue;
		Map<String, Object> w;
		try {
			w = this.userDao.getWealth(user);
			newValue = (Integer) w.get("income");
			newValue += amount * value;
			w.put("income", newValue);
			Map<String, String> m = new HashMap<String, String>();
			for (Map.Entry<String, Object> entry : w.entrySet()) {
				m.put(entry.getKey(), String.valueOf(entry.getValue()));
			}
			this.userDao.updateWealth(user, m);
			//更新用户信息
			String uid = KeyUtils.uid(user);
			BoundHashOperations<String, String, String> ops = template.boundHashOps(uid);
			ops.put("income", String.valueOf(newValue));
		} catch (Exception ex) {
			log.error("update wealth failed:" + user + "/" + value + "/"
					+ amount, ex);
		}
	}

	public void getFlower(String id, int flower) {
		String uid = KeyUtils.uid(id);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		Map<String,String> map = ops.entries();
		try {
			if (map.isEmpty()) {
				map = this.userService.findUser(id);
			}
			int flowerAmt = Integer.valueOf(map.get("flowerAmt"));
			flowerAmt += flower;
			ops.put("flowerAmt", String.valueOf(flowerAmt));
			this.userService.saveUser(id);
			synFlower(id,flower);
		} catch (Exception e) {
			log.error("User is " + uid, e);
		}
	}
	
	private void synFlower(String id,int flowerAmt) {
		Session session = this.roleManager.getSession(id);
		if(session!=null){
			Response res = new Response(OpCodes.SYN_FLOWER);
			res.setData(flowerAmt);
			session.send(res);
		}
	}

	public boolean giveFlower(String from, String to, int flower)
			throws Exception {
		// 更新赠与方
		String uid = KeyUtils.uid(from);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		int fa = Integer.valueOf(ops.get("flowerAmt"));
		if (fa - flower < 0) {
			return false;
		}

		fa -= flower;
		ops.put("flowerAmt", String.valueOf(fa));
		int pay = Integer.valueOf(ops.get("payout"));
		pay += flower;
		ops.put("payout", String.valueOf(pay));
		userService.synchroUser(this.roleManager.getSession(from));
		userDao.updateWealth(from, ops.entries());

		// 更新被赠与方
		uid = KeyUtils.uid(to);
		ops = template.boundHashOps(uid);
		int income = Integer.valueOf(ops.get("income"));
		income += flower*WealthService.ADD_FlOWER;
		ops.put("income", String.valueOf(income));
		Session toSession = this.roleManager.getSession(to);
		if(toSession!=null)userService.synchroUser(toSession);
		userDao.updateWealth(to, ops.entries());

		//String content = to + "," + String.valueOf(flower);
		//this.msgService.sendAll(rid, MessageService.GET_FLOWER, content);
		return true;
	}
}
